import Vue from 'vue'
import VueRouter from 'vue-router'
import Reg from '@/views/Reg/Reg.vue'
import Login from '@/views/Login/Login.vue'
import Main from '@/views/Main/Main.vue'
import Home from '@/views/Menus/Home/Home.vue'
import UserInfo from '@/views/Menus/User/UserInfo.vue'
import UserAvatar from '@/views/Menus/User/UserAvatar.vue'
import UserPwd from '@/views/Menus/User/UserPwd.vue'
import Artcate from '@/views/Menus/Artcle/ArtCate.vue'
import ArtList from '@/views/Menus/Artcle/ArtList.vue'
// 注册路由
Vue.use(VueRouter)

const routes = [
  // 注册的路由配置
  {
    path: '/reg',
    name: 'reg',
    component: Reg
  },
  // 登录的路由配置
  {
    path: '/login',
    name: 'login',
    component: Login
  },
  // 首页的路由配置
  {
    path: '/',
    name: 'main',
    component: Main,
    redirect: 'home', // 重定向到主页home
    children: [
      // 二级路由的嵌套的使用,二级路由path 不用加斜杠 
      {
        path: 'art-cate',
        name: 'artCate',
        component: Artcate
      },
      {
        path: 'art-list',
        name: 'artList',
        component: ArtList
      }, 
      {
        path: 'home',
        name: 'home',
        component: Home
      },
      // 去用户资料
      {
        path: 'user-info',
        name: 'userInfo',
        component: UserInfo
      },
      // 修改用户头像
      {
        path: 'user-avatar',
        name: 'userAvatar',
        component:  UserAvatar
      },
      {
        path: 'user-pwd',
        name: 'userPwd',
        component:  UserPwd
      },
    ]
  },
]

// 创建路由实例
const router = new VueRouter({
  routes
})

// VueRouter.prototype.push: push函数要求不能重复跳转路由
// 解决办法, 就是改写 VueRouter中的push函数, 但需要保留之前的功能
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}

// 暴露路由实例
export default router
